/*
 invite.go
 Created by hupfei on 2019-07-02
*/

package service

import (
	"errors"
	"strconv"
	"zimuzu_cvn_web_api/config"
	"zimuzu_cvn_web_api/model"
	"zimuzu_cvn_web_api/pkg/e"
	"zimuzu_cvn_web_api/pkg/errno"
)

func InviteInfo(uid int64) (res model.InviteInfo, err error) {
	//查询邀请人昵称
	user, err := model.GetUserByUid(config.C.DB.DB, uid)
	if err != nil {
		return
	}

	res.Username = user.Nickname
	res.Code = user.Uid

	//查询受邀人列表
	inviteList, err := model.GetInviteUserListFromDB(uid)
	if err != nil {
		return
	}

	for _, user := range inviteList {
		//手机号隐藏
		user.Name = e.NicknameFormat(user.Name)

		//累计获得cvnt
		res.Cvnt += user.Cvnt

		res.InviteList = append(res.InviteList, user)
	}
	return
}

//根据手机号查询邀请人的UID(邀请码)
func GetInviteUidByMobile(mobile string, area int) (uid string, err error) {
	uid, err = model.GetInviteUidByMoile(mobile, area)
	if err != nil {
		err = errors.New("获取邀请人邀请码失败")
	}
	return
}

//更新被邀请人的UID
//Auth: Xu
func UpdateUidByMobile(InvitationCode string, mobile string, area int, uid int64) (err error) {
	//判断邀请码是否存在
	inviteUid, _ := strconv.ParseInt(InvitationCode, 10, 64)
	res, _ := model.CheckUserExist(config.C.DB.DB, inviteUid)
	if !res {
		return errno.ErrInviteCode
	}
	inviteUidFromDb, err := model.GetInviteUidByMoile(mobile, area)
	if len(inviteUidFromDb) == 0 {
		return model.InsertInviteInfoToDB(inviteUid, area, mobile, uid)
	} else {
		err = model.UpdateUidByMoile(mobile, area, uid)
		if err != nil {
			err = errors.New("更新被邀请人UID失败")
		}
	}

	return
}

/*
 * @ Author: hupfei
 * @ Description: 填写邀请码（返佣邀请/矿工邀请），到此方法时用户一定注册了
 **/
type AcceptInviteResponse struct {
	//0 全部失败  1全部成功 2返佣成功 3矿工成功
	Status int64 `json:"status"`
}

func FillInInviteCode(uid int64, params model.AcceptInviteParams) (re AcceptInviteResponse, info string, err error) {
	//if uid == params.Code {
	//	return re, "", errors.New("您是超级节点，不能邀请自己")
	//}

	//邀请码是否存在
	ok, _ := model.CheckUserExist(config.C.DB.DB, params.Code)
	if !ok {
		return re, info, errno.ErrInviteCode
	}

	var minerCodeSuc = true
	var inviteCodeSuc = true

	////邀请码是否是超级节点邀请码
	//isSuperNodeCode, err := model.CheckUserIsSuperNode(params.Code)
	//if err != nil {
	//	return re, info, err
	//}
	//
	//if isSuperNodeCode {
	//	//成为该超级节点的矿工
	//	err = SuperInviteCode(uid, params.Code, 0)
	//	if err != nil {
	//		minerCodeSuc = false
	//		if params.Source == 1 {
	//			info = err.Error()
	//		}
	//	}
	//} else {
	//	minerCodeSuc = false
	//	if params.Source == 1 {
	//		info = errno.ErrInviteCode.Info
	//	}
	//}

	//用户是否已经填写过返佣的邀请码
	ok, err = model.CheckUserExistInCvnInviteLog(uid)
	if err != nil {
		if minerCodeSuc {
			re.Status = 3
		}
		return re, info, err
	}
	if ok {
		inviteCodeSuc = false
		if params.Source == 0 {
			info = errno.ErrInviteCode.Info
		}
	} else {
		//插入邀请表
		err = model.InsertInviteInfoToDB(params.Code, params.PhoneArea, params.Phone, uid)
		if err != nil {
			inviteCodeSuc = false
			if params.Source == 0 {
				info = errno.DbError.Info
			}
		}
	}

	if minerCodeSuc && inviteCodeSuc {
		re.Status = 1
	} else if inviteCodeSuc {
		re.Status = 2
	} else if minerCodeSuc {
		re.Status = 3
	} else {
		re.Status = 0
	}

	return re, info, nil
}
